package com.vcredit.creditcat.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.dto.creditdiagnose.ProducItemDiagnoseDto;
import com.vcredit.creditcat.dto.creditrepository.AuthItemDto;
import com.vcredit.creditcat.enumtype.DiagnosisEnum;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.framework.annotation.Log;

@Service
public class CreditDiagnoseService {

	@Autowired
	private CustProductService custProductService;

	@Autowired
	private CustProductApplyService custProductApplyService;

	@Autowired
	private MerchantProductService merchantProductService;

	/**
	 * 信用诊断
	 * 
	 * @param cust
	 * @param productIdArr
	 * @return
	 * @throws ParseException 
	 */
	@Log
	public List<ProducItemDiagnoseDto> list(Cust cust, Long[] productIdArr) throws ParseException {
		Date date = new Date();

		List<ProducItemDiagnoseDto> diagnoseResult = new ArrayList<ProducItemDiagnoseDto>();

		// 查询用户对该产品的所需的认证项以及认证状态
		for (Long productId : productIdArr) {

			boolean shortAuth = false; // 缺少认证 false为不缺少

			// 查询用户对该产品的所需认证和认证状态
			List<AuthItemDto> authItemDto = custProductService.queryProductItem(cust.getLastProccessId(), productId);

			// 查询用户对该产品的申请提交记录
			CustProductApply applyProduct = custProductApplyService.findNewApplyProduct(productId, cust.getCustId());

			// 查询产品详情
			MerchantProduct product = merchantProductService.findById(productId);

			// 检查是否有认证缺失
			for (AuthItemDto authItem : authItemDto) {
				if (authItem.getIsFinish() == 0) {
					shortAuth = true;
					break;
				}
			}

			String diagnosis = this.getDiagnosis(shortAuth, applyProduct);

			// 产品项诊断结果
			ProducItemDiagnoseDto itemDiagnoseDto = new ProducItemDiagnoseDto(productId, product.getProductName(),
					product.getProductIcon(), date, authItemDto, diagnosis);

			diagnoseResult.add(itemDiagnoseDto);
		}

		return diagnoseResult;
	}

	/**
	 * 获取诊断结果
	 * 
	 * @param shortAuth
	 * @param applyProduct
	 * @return
	 * @throws ParseException 
	 */
	private String getDiagnosis(boolean shortAuth, CustProductApply applyProduct) throws ParseException {
		String diagnosis = DiagnosisEnum.def.getDsc();
		if (shortAuth) {
			// 所需认证未完成
			diagnosis = DiagnosisEnum.no_auth.getDsc();
			return diagnosis;
		}

		if (applyProduct == null) {
			return diagnosis;
		}

		if (applyProduct.getAuditResult() == 2) {
			// 审批中
			diagnosis = DiagnosisEnum.auditing.getDsc();
		} else if (applyProduct.getAuditResult() == 1) {
			// 审批成功，是否可以再次申请和下次申请的时间来判断是否可以申请
			if (!applyProduct.getIsApply() || 
					ConcurrentDateUtil.strToDate(applyProduct.getCreditApplyTime()).after(new Date())) {
				diagnosis = DiagnosisEnum.applied.getDsc();
			}
		} else if (applyProduct.getAuditResult() == 0) {
			// 已审批，审批失败
			diagnosis = DiagnosisEnum.no_amt.getDsc() + applyProduct.getRejectMsg();
		}

		return diagnosis;
	}

}
